package com.ucode.springboot.starter.web.config;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.Validator;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
/**
 * spring mvc 公共配置基类
 * @author: liliang
 * @date: 2019年12月16日 下午9:02:30
 */
public class BaseWebMvcConfigurer implements WebMvcConfigurer{

//    @Lazy
//    @Autowired
//    private UserService userService;
    
    /**
     * 添加自定义方法参数处理器
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> arg0) {
        //Token参数解析
        
//        argumentResolvers.add(new TokenArgumentResolver(userService));
    }

    /**
     * 跨域支持
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
       registry.addMapping("/**")
        .allowedOrigins("*")//允许所有的请求域名访问跨域资源，当设置具体URL时只有被设置的url可以跨域访问例如：allowedOrigins(“https://www.baidu.com”),则只有https://www.baidu.com能访问跨域资源
        .allowedMethods("*")
        .allowedHeaders("*")
        .allowCredentials(true);
    }

    /**
     * 添加类型转换器和格式化器
     * @param registry
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        
    }

    /**
     * 静态资源映射
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    }

    @Override
    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> arg0) {
        
    }

    @Override
    public void addViewControllers(ViewControllerRegistry arg0) {
        
    }

    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer arg0) {
        
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer arg0) {
        
    }

    @Override
    public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> arg0) {
        
    }

    /**
     *  修改自定义消息转换器
     * @param converters
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(Charset.forName("utf-8"));
//        
//        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
//        ObjectMapper mapper = new ObjectMapper();
//        //序列换成json时,将所有的long变成string 因为js中得数字类型不能包含所有的java long值
//        SimpleModule simpleModule = new SimpleModule();
//        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
//        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
//        mapper.registerModule(simpleModule);
//        /**
//         * Include.ALWAYS 默认
//         * Include.NON_DEFAULT 属性为默认值不序列化
//         * Include.NON_EMPTY 属性为 空（""） 或者为 NULL 都不序列化，则返回的json是没有这个字段的。这样对移动端会更省流量
//         * Include.NON_NULL 属性为NULL 不序列化,就是为null的字段不参加序列化
//         */
//        mapper.setSerializationInclusion(Include.NON_NULL);
//        
//        // 字段保留，将null值转为""
////        mapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>(){
////          @Override
////          public void serialize(Object o, JsonGenerator jsonGenerator,SerializerProvider serializerProvider)
////                  throws IOException, JsonProcessingException{
////              jsonGenerator.writeString("");
////          }
////        });
//        
//        mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
//        mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
//        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//        //设置日期格式  
//        mapper.setDateFormat(UcodeDateFormat.instance);  
//        jackson2HttpMessageConverter.setObjectMapper(mapper);  
//        //设置中文编码格式  
//        List<MediaType> list = new ArrayList<MediaType>();  
//        list.add(MediaType.APPLICATION_JSON_UTF8);
//        jackson2HttpMessageConverter.setSupportedMediaTypes(list);
//        
////      converters.add(0,stringHttpMessageConverter);//最高优先级
//        converters.add(stringHttpMessageConverter);//最低优先级
//        converters.add(jackson2HttpMessageConverter);
    }

    @Override
    public void configurePathMatch(PathMatchConfigurer arg0) {
        
    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry arg0) {
        
    }

    @Override
    public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> arg0) {
        
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> arg0) {
        
    }

    @Override
    public MessageCodesResolver getMessageCodesResolver() {
        return null;
    }

    @Override
    public Validator getValidator() {
        return null;
    }

}
